package agnewpct;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;


public class agNewpct {

	private JFrame frame;
	private JTextField textField;
	private DefaultListModel<String> listModel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					agNewpct window = new agNewpct();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public agNewpct() {
		initialize();

		ClipboardTextListener ctl = new ClipboardTextListener () { 
            @Override 
            protected void textoHaCambiado(String nuevoTexto) {
            	if (!nuevoTexto.equals(textField.getText())) {
	            	textField.setText(nuevoTexto);
	            	if (nuevoTexto.contains("linkbucks")) {
	            		String sDescarga = nuevoTexto.substring(nuevoTexto.indexOf("http", 4));
	            		listModel.addElement(sDescarga);
	            		try {
	            			String cmd = "C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe ";
	            			cmd += " "+sDescarga;
	            			Runtime.getRuntime().exec(cmd); 
	            		} catch (IOException ioe) {
	            			System.out.println (ioe);
	            		}
	            	}
            	} else {
            		System.out.println("No ha cambiado");
            	}
            } 
        };
        ctl.comienzaAEscuchar();
        

		
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 1100, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		textField = new JTextField();
		frame.getContentPane().add(textField, BorderLayout.NORTH);
		textField.setColumns(10);
		
        
        listModel = new DefaultListModel<String>();
        
		JList<String> list = new JList<String>(listModel);
		frame.getContentPane().add(list, BorderLayout.CENTER);
	}

}
